var cfg = require('../../utils/cfg');

var socketUtil = require('../../utils/socketUtil');


var app = getApp();

var app = getApp();
Page({
  data:{
    fromId : '',
    roomTicket : '',
    openId : '',
    masterAva : ''
  },
  onLoad:function(options){
    console.log('welcome onload fromId=' + options.fromId);
    this.data.fromId = options.fromId;
    console.log('>>>>>>>fromId;' + options.fromId);
    this.data.roomTicket = options.roomTicket;
    this.data.openId = options.openId;
    this.data.masterAva = options.masterAva;

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  saveUserInfo: function (nickName, picUrl, openId) {
    let that  = this;

    wx.request({
      url: cfg.host + cfg.mwurl.saveUserInfo,
      data: {
        // openId: wx.getStorageSync('loginUser').openId,
        openId: openId,
        nickName: nickName,
        picUrl: picUrl
      },
      success: function (res) {
        if (res.data.code !== 1000) {
          return;
        }

        that.afterProcess();

        console.log(">>>>保存用户信息到后台");
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST'
    })
  },

  processUserInfo : function(nickName, picUrl) {
    let that  = this;
    if (wx.getStorageSync('loginUser')) {
      that.saveUserInfo(nickName, picUrl, wx.getStorageSync('loginUser').openId);
    } else {
      wx.login({
        success: function (res) {
          console.log('code=' + res.code);
          if (res.code) {
            wx.request({
              url: cfg.host + cfg.mwurl.wxLogin,
              data: { code: res.code },
              success: function (resObj) {
                console.log('服务器返回数据:' + resObj.data);
                if (1000 !== resObj.data.code) {
                  console.log(resObj.data.message);
                  return;
                }

                console.log('>>>>>>>>保存登录用户loginUser信息:');
                wx.setStorageSync('loginUser', resObj.data.data);
                socketUtil.socketInit();

                that.saveUserInfo(nickName, picUrl, resObj.data.data.openId);
              }
            })
          } else {
            console.log('获取用户信息失败');
          }
        }
      })
    }
  },

  afterProcess : function() {
    let that = this;

    if (that.data.fromId == 1000) {
      wx.reLaunch({
        url: '/pages/home/home',
        success: function () {
          console.log('跳转到home页面');
        }
      });
    }

    if (that.data.fromId == 1001) {
      wx.reLaunch({
        url: '/pages/readyOther/readyOther?roomTicket=' + that.data.roomTicket + '&openId=' + that.data.openId + '&masterAva=' + that.data.masterAva,
        success: function () {
          console.log('跳转到readyOther页面');
        }
      });
    }

    if (that.data.fromId == 1002) {
      console.log('>>>>>>>跳转到moreReadyOther页面');
      wx.reLaunch({
        url: '/pages/moreReadyOther/moreReadyOther?roomTicket=' + that.data.roomTicket + '&openId=' + that.data.openId + '&masterAva=' + that.data.masterAva,
        success: function () {
          console.log('跳转到moreReadyOther页面');
        }
      });
    }
  },
  onGotUserInfo : function(e) {
    console.log(">>>>>>>>>onGotUserInfo");
    let that = this;
    if (!e.detail.userInfo) {
        return;
    }

    wx.setStorageSync('userInfo', e.detail.userInfo);

    app.globalData.userInfo = e.detail.userInfo;
    console.log('保存成功');
    let userinfo = app.globalData.userInfo;

    that.processUserInfo(userinfo.nickName, userinfo.avatarUrl);
  }
  
})